﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Neuron.Cell;
using Neuron.Cell.Control;


namespace Neuron.NetworkDesigner
{
    public partial class ctrlStatistic : UserControl
    {

        private ctrlPerceptronDrawingBoard _drawingBoard;

        private int _drawingX = 0;
        private int _drawingPerceptronY = 0;
        private int _drawingNetworkY = 0;
        private int _stepWidth = 0;
        private Graphics _graphicsMonitor = null;

        private List<double> _valuesPerceptron = new List<double>();
        private List<double> _valuesNetwork = new List<double>();

        private Perceptron _selectedPerceptron = null;


        public ctrlStatistic()
        {
            InitializeComponent();

            toolTipStatistic.SetToolTip(comboBoxPerceptrons, "Choose to track the whole network or a single perceptron.");
            toolTipStatistic.SetToolTip(checkBoxTrack, "Activate checkbox to enable tracking.");
            toolTipStatistic.SetToolTip(buttonClear, "Clears the graph-window.");
        }

        public ctrlPerceptronDrawingBoard DrawingBoard
        {
            set
            {
                this._drawingBoard = value;

                this.updatePerceptronComboBox(0);
                this._graphicsMonitor = panelMonitor.CreateGraphics();
                this._stepWidth = 5;
                this.CleanMonitor();
            }
        }

        public void updatePerceptronComboBox(int numberOfPerceptrons)
        {
            int selectedIndex = comboBoxPerceptrons.SelectedIndex;
            comboBoxPerceptrons.Items.Clear();
            comboBoxPerceptrons.Items.Add("NETWORK");
            foreach (Control ctrl in this._drawingBoard.MainScreen.Controls)
            {
                if (ctrl is ctrlPerceptron)
                {
                    Perceptron p = ((ctrlPerceptron)ctrl).Perceptron;
                    if ((p.ActivationFunction != ActivationFunctionEnum.actor) && (p.ActivationFunction != ActivationFunctionEnum.receptor))
                    {
                        comboBoxPerceptrons.Items.Add(p.Name);
                    }
                }
            }
            if ((comboBoxPerceptrons.SelectedIndex > comboBoxPerceptrons.Items.Count) || (comboBoxPerceptrons.SelectedIndex < 0))
            {
                comboBoxPerceptrons.SelectedIndex = comboBoxPerceptrons.Items.Count - 1;
            }
            else
            {
                comboBoxPerceptrons.SelectedIndex = selectedIndex;
            }
        }

        private void CleanMonitor()
        {
            panelMonitor.Refresh();

            System.Drawing.Pen myPen;
            myPen = new System.Drawing.Pen(System.Drawing.Color.Gray);
            myPen.Width = 1;
            this._graphicsMonitor.DrawLine(myPen, 0, panelMonitor.Height / 2, panelMonitor.Width, panelMonitor.Height / 2);

            myPen = new System.Drawing.Pen(System.Drawing.Color.LightGray);
            myPen.Width = 1;
            for (int i = 0; i < panelMonitor.Width; i+=this._stepWidth)
            {
                this._graphicsMonitor.DrawLine(myPen, i, 0, i, panelMonitor.Height);
            }
        }

        public void calculateCriteriaDelta()
        {
            double criteriaDeltaNetwork = 0.0;
            double criteriaDeltaPerceptron = 0.0;

            if (!checkBoxTrack.Checked) return;

            System.Drawing.Pen myPen;

            foreach (Control ctrl in this._drawingBoard.MainScreen.Controls)
            {
                Perceptron p1 = ((ctrlPerceptron)ctrl).Perceptron;
                criteriaDeltaNetwork += Math.Round(p1.CriteriaDelta, 5);
                //toolStripStatusLabel2.Text = "Network-Delta: " + Convert.ToString(criteriaDelta);
            }
            this._valuesNetwork.Add(criteriaDeltaNetwork);

            myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
            myPen.Width = 2;
            this._graphicsMonitor.DrawLine(myPen, this._drawingX, this._drawingNetworkY, this._drawingX + this._stepWidth, Convert.ToInt32(Math.Round((panelMonitor.Height / 2) - (criteriaDeltaNetwork * this._stepWidth))));

            this._drawingNetworkY = Convert.ToInt32(Math.Round((panelMonitor.Height / 2) - (criteriaDeltaNetwork * this._stepWidth)));

            if (comboBoxPerceptrons.Text != "NETWORK")
            {
                criteriaDeltaPerceptron = Math.Round(this._selectedPerceptron.CriteriaDelta, 5);
                this._valuesPerceptron.Add(criteriaDeltaPerceptron);

                myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
                myPen.Width = 2;
                this._graphicsMonitor.DrawLine(myPen, this._drawingX, this._drawingPerceptronY, this._drawingX + this._stepWidth, Convert.ToInt32(Math.Round((panelMonitor.Height / 2) - (criteriaDeltaPerceptron * this._stepWidth))));

                this._drawingPerceptronY = Convert.ToInt32(Math.Round((panelMonitor.Height / 2) - (criteriaDeltaPerceptron * this._stepWidth)));
            } 
            else
            {
                this._valuesPerceptron.Add(0.0);
            }


            if (this._drawingX < panelMonitor.Width)
            {
                this._drawingX += (this._stepWidth);
            }
            else
            {
                this.CleanMonitor();
                this._drawingX = 0;
            }
        }

        private void buttonZoomUp_Click(object sender, EventArgs e)
        {
            this._stepWidth += 1;
            this.CleanMonitor();
        }

        private void buttonZoomDown_Click(object sender, EventArgs e)
        {
            if (this._stepWidth > 5) this._stepWidth -= 1;
            this.CleanMonitor();
        }

        private void comboBoxPerceptrons_SelectedValueChanged(object sender, EventArgs e)
        {
            foreach (Control ctrl in this._drawingBoard.MainScreen.Controls)
            {
                if (ctrl is ctrlPerceptron)
                {
                    if (((ctrlPerceptron)ctrl).Perceptron.Name == comboBoxPerceptrons.Text)
                    {
                        this._selectedPerceptron = ((ctrlPerceptron)ctrl).Perceptron;
                    }
                }
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            this.CleanMonitor();
            this._valuesNetwork.Clear();
            this._valuesPerceptron.Clear();
        }

        private void panelMonitor_Resize(object sender, EventArgs e)
        {
            this._graphicsMonitor = panelMonitor.CreateGraphics();
        }
    }
}
